package com.qqtech.qquan.mall.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.common.annotation.VisitAnnt;
import com.qqtech.core.common.enums.RespEnum;
import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.mall.enums.MallCommentLevelDictEnum;
import com.qqtech.qquan.mall.enums.MallCommentScoreDictEnum;
import com.qqtech.qquan.mall.model.MallComment;
import com.qqtech.qquan.mall.model.MallCommentQuery;
import com.qqtech.qquan.mall.model.MallCommentVo;
import com.qqtech.qquan.mall.service.MallCommentService;
import com.qqtech.qquan.order.enums.OrderBaseOrderStatusDictEnum;
import com.qqtech.qquan.order.model.OrderBase;
import com.qqtech.qquan.order.model.OrderBaseQuery;
import com.qqtech.qquan.order.model.OrderItemQuery;
import com.qqtech.qquan.order.model.OrderItemVo;
import com.qqtech.qquan.order.service.OrderBaseService;
import com.qqtech.qquan.order.service.OrderItemService;
import com.qqtech.qquan.user.model.UserBaseVo;
import com.qqtech.qquan.user.service.UserBaseService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/mall/comment")
public class MallCommentController extends BaseWebController<MallComment, MallCommentQuery> {
	@Resource
	private MallCommentService mallCommentService;
	
	@Resource
	private OrderItemService orderItemService;
	
	@Resource
	private OrderBaseService orderBaseService;
	
	@Resource
	private UserBaseService userBaseService;
    
	@Override
	protected BaseService<MallComment> getBaseService() {
		return mallCommentService;
	}
	
	/**
	 * 评价列表
	 * 
	 * @param orderBaseQuery
	 * @return
	 */
	@RequestMapping(value = "/page")
	@ResponseBody
	public Result page(@RequestBody MallCommentQuery mallCommentQuery) {
		Result result = new Result();
		if (mallCommentQuery == null) {
			result.addErr("参数不能为空");
		}else {
			if (mallCommentQuery.getProductId() == null) {
				result.addErr("商品数据错误");
			}
		}
		if (result.checkForm()) {
			mallCommentQuery.setStatus(YesNoEnum.YES.getCode());
			Pageable pageKit = new PageKit(mallCommentQuery.getPage(), new PageOrder(Direction.DESC, "newAt"));
			Page<MallCommentVo> page = mallCommentService.queryPageList(mallCommentQuery, pageKit);
			List<MallCommentVo> mallCommentList = page.getContent();
			if(mallCommentList != null && mallCommentList.size() > 0){
				for(MallCommentVo commentVo : mallCommentList){
		 			UserBaseVo userVo = userBaseService.getById(commentVo.getCreatorId());
		 			commentVo.setCreatorImg( userVo.getLogo());
		 			commentVo.setCreatorName(userVo.getNick());
				}
			}
			result.put("page", page);
		}
		return result;
	}
	
	/**
	 * 添加商品评价页面入口
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/toAdd")
	@ResponseBody
	public Result toAdd(@RequestBody OrderBaseQuery orderBaseQuery) {
		Result result = new Result();
		if (orderBaseQuery == null || StringUtil.isBlank(orderBaseQuery.getOrderNo())) {
			result.addErr("参数不能为空");
		}
		if (result.checkForm()) {
			//未评论的订单明细
			OrderItemQuery orderItemQuery = new OrderItemQuery();
			orderItemQuery.setOrderNo(orderBaseQuery.getOrderNo());
			orderItemQuery.setIsComment(YesNoEnum.NO.getCode());
			Pageable pageKit = new PageKit(orderBaseQuery.getPage(), new PageOrder(Direction.DESC, "id"));
			Page<OrderItemVo> page = orderItemService.queryPageList(orderItemQuery, pageKit);
			List<OrderItemVo> itemList = page.getContent();
			if(itemList != null && itemList.size() > 0){
				for(int i = 0; i < itemList.size(); i++){
					OrderItemVo vo = itemList.get(i);
					vo.setIndex(i);
				}
			}
			result.put("itemList", itemList);
		}
		return result;
	}
	
	/**
	 * 添加商品评价
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/doAdd")
	@ResponseBody
	public Result doAdd(@RequestBody MallComment mallComment) {
		Result result = new Result();
		if (mallComment == null || StringUtil.isBlank(mallComment.getOrderNo()) || mallComment.getProductId() == null ) {
			result.addErr("参数不能为空");
			return result;
		}
		if (mallComment.getType() == null  ) {
			result.addErr("评论业务类型为空");
			return result;
		}
		if(mallComment.getScore() == null || StringUtil.isBlank(mallComment.getContent())){
			result.addErr("评价内容不能为空");
			return result;
		}
		OrderBase order = orderBaseService.getByOrderNo(mallComment.getOrderNo());
		if(order == null || !super.getUserId().equals(order.getBuyerId())){
			result.addErr("评价参数错误");
			return result;
		}
		if (super.getUserId().intValue() != order.getBuyerId().intValue()){
			result.addErr("无权限添加评价");
			return result;
		}
		if(order.getOrderStatus() != null && order.getOrderStatus().intValue() != OrderBaseOrderStatusDictEnum.COMPLETED.getCode()){
			result.addErr("订单未完成");
			return result;
		}
		if(mallComment.getScore() > MallCommentScoreDictEnum.SCORE5.getCode() ||  mallComment.getScore() < MallCommentScoreDictEnum.SCORE1.getCode()){
			result.addErr("评分参数错误");
			return result;
		}
		
		//评分对应的等级
		if(mallComment.getScore() == MallCommentScoreDictEnum.SCORE1.getCode()){
			mallComment.setLevel(MallCommentLevelDictEnum.BAD.getCode());
		}else if(mallComment.getScore() == MallCommentScoreDictEnum.SCORE2.getCode()){
			mallComment.setLevel(MallCommentLevelDictEnum.GENERAL.getCode());
		}else if(mallComment.getScore() >= MallCommentScoreDictEnum.SCORE3.getCode()){
			mallComment.setLevel(MallCommentLevelDictEnum.GOOD.getCode());
		}
		
		mallComment.setShopId(order.getShopId());
		mallComment.setAnonymous(YesNoEnum.YES.getCode());
		mallComment.setStatus(YesNoEnum.YES.getCode());
		mallComment.setCreatorId(super.getUserId());
		mallComment.setNewAt(TimeUtil.now());
		mallComment.setUpdAt(TimeUtil.now());
		mallCommentService.handleSaveComment(mallComment);
		return result;
	}
}
